<?php

namespace App\Jobs\SubAccount;

use App\Models\Business\ShiftSetting;
use App\Models\SubAccount\SubAccount;
use App\Models\SubAccount\SubAccountLoginLogoutRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

//生成上号下号记录
class SubAccountLoginLogoutRecordJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected array $shiftNames=['早班','晚班'];

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $shiftNames=$this->shiftNames;
        $accounts=$this->getSubAccounts();
        if($accounts && count($accounts) && $shiftNames && count($shiftNames)){
            foreach ($accounts as $account){
                if($this->checkLogs($account)==0){
                    foreach ($shiftNames as $shiftName){
                        $data=$this->loginLogoutData($account, $shiftName);
                        SubAccountLoginLogoutRecord::create($data);
                    }
                }
            }
        }

    }

    //是否已生成过
    public function checkLogs($account){
        $recordCode=date('Ymd').$account->id;
        return SubAccountLoginLogoutRecord::where('record_code',$recordCode)->count('id');
    }

    //
    public function loginLogoutData($subAccount, $shiftName){
        return [
            'sub_account_id'=>$subAccount->id,
            'sub_account'=>$subAccount->sub_account,
            'log_in_out_date'=>date('Y-m-d'),
            'record_code'=>date('Ymd').$subAccount->id,
            'template_id'=>$subAccount->template_id,
            'shop_id'=>$subAccount->shop->id,
            'shop_name'=>$subAccount->shop->shop_name,
            'department1_id'=>$subAccount->department1_id??null,
            'department1'=>$subAccount->department1->name??null,
            'department2_id'=>$subAccount->department2_id??null,
            'department2'=>$subAccount->department2->name??null,
            'department3_id'=>$subAccount->department3_id??null,
            'department3'=>$subAccount->department3->name??null,
            'department_id'=>$subAccount->department_id??null,
            'shift_name'=>$shiftName,
            'on_off_time'=>$this->getOnOffTime($subAccount,$shiftName),
        ];
    }

    //符合条件的账号
    public function getSubAccounts(){
        $where=[
            ['status','=',1],
//            ['service_time_start','<=',now()],
//            ['service_time_end','>=',now()],
        ];
//        $orWhere=[
//            ['status','=',1],
//            ['try_time_start','<=',now()],
//            ['try_time_end','>=',now()],
//        ];
        $subs=SubAccount::where($where)
//            ->orWhere($orWhere)
            ->get();
        return $subs;
    }
    //获取上下班时间
    public function getOnOffTime($subAccount,$shiftName){
        $shift=$subAccount->shop->departmentTwo->shift??null;
        if($shift){
            if($shiftName=='早班'){
                return $shift->day_shift_start.'-'.$shift->day_shift_end;
            }elseif ($shiftName=='晚班'){
                return $shift->night_shift_start.'-'.$shift->night_shift_end;
            }
        }
        return '';
    }

}
